﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestTasks
{
    public class ReducedHeap
    {
        public IEnumerable<int> SelectTopThree(IEnumerable<int> stream) 
        {
            if (stream == null) throw new ArgumentException();
            return SelectTopThreeImpl(stream);
        }

        IEnumerable<int> SelectTopThreeImpl(IEnumerable<int> stream) 
        {
            int? a = null, b = null, c = null;

            foreach (var item in stream)
            {
                if (Nullable.Compare(a,item) < 0)
                {
                    c = b;
                    b = a;
                    a = item;
                }
                else if (Nullable.Compare(b, item) < 0) 
                {
                    c = b;
                    b = item;
                }
                else if (Nullable.Compare(c, item) < 0) 
                {
                    c = item;
                }
            }

            if (a.HasValue) yield return a.Value;
            if (b.HasValue) yield return b.Value;
            if (c.HasValue) yield return c.Value;
        }
    }
}
